package cn.csc.module.shop.dal.mysql.user;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.mybatis.core.mapper.BaseMapperX;
import cn.csc.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csc.module.shop.controller.admin.user.vo.ShopUserExportReqVO;
import cn.csc.module.shop.controller.admin.user.vo.ShopUserPageReqVO;
import cn.csc.module.shop.dal.dataobject.user.ShopUserDO;
import org.apache.ibatis.annotations.Mapper;

import java.math.BigInteger;
import java.util.List;

/**
 * 商家用户 Mapper
 *
 * @author liuchuang
 */
@Mapper
public interface ShopUserMapper extends BaseMapperX<ShopUserDO> {

    default PageResult<ShopUserDO> selectPage(ShopUserPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ShopUserDO>()
                .eqIfPresent(ShopUserDO::getShopId, reqVO.getShopId())
                .eqIfPresent(ShopUserDO::getUserId, reqVO.getUserId())
                .likeIfPresent(ShopUserDO::getNickName, reqVO.getNickName())
                .eqIfPresent(ShopUserDO::getCode, reqVO.getCode())
                .eqIfPresent(ShopUserDO::getPhoneNumber, reqVO.getPhoneNumber())
                .eqIfPresent(ShopUserDO::getIsAdmin, reqVO.getIsAdmin())
                .eqIfPresent(ShopUserDO::getHasAccount, reqVO.getHasAccount())
                .betweenIfPresent(ShopUserDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ShopUserDO::getId));
    }

    default List<ShopUserDO> selectList(ShopUserExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ShopUserDO>()
                .eqIfPresent(ShopUserDO::getShopId, reqVO.getShopId())
                .eqIfPresent(ShopUserDO::getUserId, reqVO.getUserId())
                .likeIfPresent(ShopUserDO::getNickName, reqVO.getNickName())
                .eqIfPresent(ShopUserDO::getCode, reqVO.getCode())
                .eqIfPresent(ShopUserDO::getPhoneNumber, reqVO.getPhoneNumber())
                .eqIfPresent(ShopUserDO::getIsAdmin, reqVO.getIsAdmin())
                .eqIfPresent(ShopUserDO::getHasAccount, reqVO.getHasAccount())
                .betweenIfPresent(ShopUserDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ShopUserDO::getId));
    }

    default ShopUserDO selectByUserId(BigInteger userId){
        return selectOne(new LambdaQueryWrapperX<ShopUserDO>()
            .eq(ShopUserDO::getUserId, userId)
        );
    }


    default ShopUserDO selectByShopId(BigInteger shopId){
        return selectOne(new LambdaQueryWrapperX<ShopUserDO>()
                .eq(ShopUserDO::getShopId, shopId)
                .eq(ShopUserDO::getIsAdmin, (byte)1)
                .last("limit 1")
        );
    }

}
